package com.unicom.lottery.controller;

import com.unicom.lottery.entity.ReturnMsg;
import com.unicom.lottery.entity.input.FillAddressInfo;
import com.unicom.lottery.entity.input.LotteryUser;
import com.unicom.lottery.entity.input.User;
import com.unicom.lottery.entity.out.LotteryNameRecord;
import com.unicom.lottery.services.LotteryServices;
import com.unicom.lottery.util.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("lottery")
@Api(value ="联通抽奖项目", description = "抽奖相关Api")
public class LotteryController {

    @Autowired
    LotteryServices lotteryServices;


    /**
     * 初始化商品信息记录
     * @return
     */
    @RequestMapping(value = "/init",method ={RequestMethod.POST})
    @ApiOperation(notes = "初始化抽奖信息2222", response = LotteryNameRecord.class, httpMethod = "POST", value = "个人中心获取接口信息")
    @ResponseBody
    public ReturnMsg initLottery(){
        return lotteryServices.initLottery();
    }


    /**
     * 添加用户信息
     * @return
     */
    @RequestMapping(value = "/addShareInfo",method ={RequestMethod.POST})
    @ApiOperation(notes = "添加用户分享信息", response = ReturnMsg.class, httpMethod = "POST", value = "添加用户分享信息")
    @ResponseBody
    public ReturnMsg addShareInfo(@RequestBody LotteryUser user){
        String keySecert="lottery@#$$@2018";
        String sign=user.getSign();
        String tranId=user.getTranId();

        String newSign= MD5Util.crypt(tranId+keySecert);
        if(!sign.equals(newSign))
           return  new ReturnMsg("ERR","sign验证失败！");

        return lotteryServices.addShareInfo(user);
    }

    /**
     * 初始化用户中奖信息
     * @return
     */
    @RequestMapping(value = "/initUserLotteryInfo",method ={RequestMethod.POST})
    @ApiOperation(notes = "初始化用户抽奖信息", response = ReturnMsg.class, httpMethod = "POST", value = "初始化用户抽奖信息")
    @ResponseBody
    public ReturnMsg initUserLotteryInfo(@RequestBody User user){
        return lotteryServices.initUserLotteryInfo(user);
    }

    /**
     * 得到用户抽奖配置信息
     * @return
     */
    @RequestMapping(value = "/userRecord",method ={RequestMethod.POST})
    @ApiOperation(notes = "用户中奖信息接口", response = LotteryNameRecord.class, httpMethod = "POST", value = "用户的中奖信息信息")
    @ResponseBody
    public  ReturnMsg userGiftRecords(@RequestBody User user){
        return lotteryServices.userGiftRecords(user);
    }


    /**
     * 填写礼品记录
     * @return
     */
    @RequestMapping(value = "/fillLotteryRecord",method =RequestMethod.POST)
    @ResponseBody
    @ApiOperation("填写中奖商品地址信息")
    public  ReturnMsg fillLotteryRecord(@RequestBody FillAddressInfo fillAddressInfo){
        String keySecert="lottery@#$$@2018";
        String sign=fillAddressInfo.getSign();
        String tranId=fillAddressInfo.getTranId();

        String newSign= MD5Util.crypt(tranId+keySecert);

        if(!sign.equals(newSign))
            return  new ReturnMsg("ERR","sign验证失败！");


        return lotteryServices.fillLotteryRecord(fillAddressInfo);
    }


    /**
     * 产品描述接口
     */
    @RequestMapping(value = "/activityDesc",method =RequestMethod.POST)
    @ResponseBody
    @ApiOperation(notes ="商品描述信息",response = ReturnMsg.class, httpMethod = "POST",value = "商品描述信息")
    public  ReturnMsg  activityDesc(){
        String msg= lotteryServices.activityDesc("activityDesc");
        return  new ReturnMsg("SUC",msg);
    }


    /**
     * 用户抽取用户奖品
     * @return
     */
    @RequestMapping(value = "/lottery",method =RequestMethod.POST)
    @ResponseBody
    @ApiOperation(notes ="调用中奖记录",response = ReturnMsg.class, httpMethod = "POST",value = "用户抽取奖品调用接口")
    public  ReturnMsg lottery(@RequestBody LotteryUser user) {
        String keySecert="lottery@#$$@2018";
        String sign=user.getSign();
        String tranId=user.getTranId();

        String newSign= MD5Util.crypt(tranId+keySecert);

        if(!sign.equals(newSign))
              return  new ReturnMsg("ERR","sign验证失败！");

        try {
            return  lotteryServices.lottery(user);
        } catch (RuntimeException e) {
            return  new ReturnMsg("ERR",e.getMessage());
        }

    }

}
